﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YJKRevitWndr.InsertDoorAndWindow.Forms
{
	public partial class ChoiceFamilyForm : System.Windows.Forms.Form
	{
		public Image Image2D
		{
			get
			{
				return this.m_Image2D;
			}
			set
			{
				this.m_Image2D = value;
			}
		}

		public string Image2DPath
		{
			get
			{
				return this.m_Image2DPath;
			}
			set
			{
				this.m_Image2DPath = value;
			}
		}

		public Image Image3D
		{
			get
			{
				return this.m_Image3D;
			}
			set
			{
				this.m_Image3D = value;
			}
		}

		public string Image3DPath
		{
			get
			{
				return this.m_Image3DPath;
			}
			set
			{
				this.m_Image3DPath = value;
			}
		}

		[DllImport("user32.dll", CharSet = CharSet.Auto)]
		public static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

		public ChoiceFamilyForm(ExternalCommandData cmdData, BuiltInCategory builtInCategory)
		{
			this.InitializeComponent();
			this.listBox.DrawMode = DrawMode.OwnerDrawVariable;
			this.listBox.DrawItem += this.ListBoxGroupRange_DrawItem;
			this.m_doc = cmdData.Application.ActiveUIDocument.Document;
			this.m_builtInCategory = builtInCategory;
			string appRootPath = AssistFunc.GetAppRootPath();
			this.m_strFullPath = appRootPath + "\\FamilyLib\\FamilyImage\\";
			if ((int)this.m_builtInCategory == -2000023)
			{
				this.m_strFullPath += "\\DoorImage\\";
			}
			else
			{
				this.m_strFullPath += "\\WindowImage\\";
			}
			this.InitFamilyListByLib();
			this.ReadData();
		}

		private void ListBoxGroupRange_DrawItem(object sender, DrawItemEventArgs e)
		{
			e.Graphics.FillRectangle(new SolidBrush(e.BackColor), e.Bounds);
			if (e.Index >= 0)
			{
				StringFormat stringFormat = new StringFormat();
				stringFormat.LineAlignment = StringAlignment.Center;
				e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds, stringFormat);
			}
			e.DrawFocusRectangle();
		}

		public void LoadImageDefault()
		{
			this.m_strImageName = this.listBox.SelectedItem.ToString();
			int num = 0;
			foreach (KeyValuePair<string, Image> keyValuePair in this.GetAllImage3D(this.m_strImageName))
			{
				if (num == this.m_nSelectIndex)
				{
					string strFamilyName = keyValuePair.Key.Substring(0, keyValuePair.Key.Length - 2);
					string path = Path.Combine(this.m_strFullPath, this.m_strImageName + "截图");
					string str = keyValuePair.Key.Substring(0, keyValuePair.Key.Length - 1);
					string image3DPath = Path.Combine(path, str + "3.jpg");
					string text = Path.Combine(path, str + "2.jpg");
					this.Image3D = keyValuePair.Value;
					this.Image2D = Image.FromFile(text, true);
					this.Image2DPath = text;
					this.Image3DPath = image3DPath;
					this.m_strFamilyName = strFamilyName;
					break;
				}
				num++;
			}
		}

		private void InitFamilyListByLib()
		{
			string[] directories = Directory.GetDirectories(this.m_strFullPath);
			new List<string>();
			foreach (string text in directories)
			{
				int num = text.LastIndexOf("\\");
				string text2 = text.Substring(num + 1);
				string text3 = text2.Substring(0, text2.Length - 2);
				if (text3 == "平开门")
				{
					this.listBox.Items.Insert(0, text3);
				}
				else if (text3 == "平开窗")
				{
					this.listBox.Items.Insert(0, text3);
				}
				else if (text3 == "固定窗")
				{
					this.listBox.Items.Insert(1, text3);
				}
				else
				{
					this.listBox.Items.Add(text3);
				}
			}
		}

		public Dictionary<string, Image> GetAllImage3D(string strFamilyName)
		{
			Dictionary<string, Image> dictionary = new Dictionary<string, Image>();
			try
			{
				foreach (string text in Directory.GetFiles(Path.Combine(this.m_strFullPath, strFamilyName + "截图"), "*.jpg"))
				{
					if (text.Contains("_1") || text.Contains("-1"))
					{
						int num = text.LastIndexOf("\\");
						int num2 = text.LastIndexOf(".");
						string key = text.Substring(num + 1, num2 - num - 1);
						Image value = Image.FromFile(text, true);
						dictionary.Add(key, value);
					}
				}
			}
			catch (Exception)
			{
			}
			return dictionary;
		}

		private void listBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.listView1.Clear();
			this.imageList1.Images.Clear();
			this.m_strImageName = this.listBox.SelectedItem.ToString();
			Dictionary<string, Image> allImage3D = this.GetAllImage3D(this.m_strImageName);
			int num = 0;
			foreach (KeyValuePair<string, Image> keyValuePair in allImage3D)
			{
				this.imageList1.Images.Add(keyValuePair.Value);
				ListViewItem listViewItem = new ListViewItem();
				string text = keyValuePair.Key.Substring(0, keyValuePair.Key.Length - 2);
				listViewItem.ImageIndex = num;
				listViewItem.Text = text;
				listViewItem.Tag = keyValuePair.Value;
				this.listView1.Items.Add(listViewItem);
				num++;
			}
		}

		private void ChoiceFamilyForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SaveData();
			base.DialogResult = DialogResult.Cancel;
		}

		private void SaveData()
		{
			string path = string.Empty;
			string str = Product.UserDataLocation + "\\";
			if ((int)this.m_builtInCategory == -2000023)
			{
				path = str + "DoorImage.dat";
			}
			else
			{
				path = str + "WinImage.dat";
			}
			FileStream fileStream = new FileStream(path, FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.listBox.SelectedIndex);
			if (this.listView1.SelectedItems.Count != 0)
			{
				streamWriter.WriteLine(this.listView1.SelectedItems[0].Index);
			}
			else
			{
				streamWriter.WriteLine(0);
			}
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void ReadData()
		{
			try
			{
				string path = string.Empty;
				string str = Product.UserDataLocation + "\\";
				if ((int)this.m_builtInCategory == -2000023)
				{
					path = str + "DoorImage.dat";
				}
				else
				{
					path = str + "WinImage.dat";
				}
				if (!File.Exists(path))
				{
					this.listBox.SelectedIndex = 0;
					this.listView1.Items[0].Selected = true;
					this.m_nSelectIndex = 0;
				}
				else
				{
					FileStream fileStream = new FileStream(path, FileMode.Open);
					StreamReader streamReader = new StreamReader(fileStream);
					streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
					int selectedIndex = Convert.ToInt32(streamReader.ReadLine());
					int num = Convert.ToInt32(streamReader.ReadLine());
					this.m_nSelectIndex = num;
					this.listBox.SelectedIndex = selectedIndex;
					this.listView1.SelectedItems.Clear();
					this.listView1.Items[num].Selected = true;
					streamReader.Close();
					fileStream.Close();
				}
			}
			catch (Exception)
			{
				this.listBox.SelectedIndex = 0;
				this.listView1.Items[0].Selected = true;
			}
		}

		private void ChoiceFamilyForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			ChoiceFamilyForm.SendMessage(this.listView1.Handle, this.LVM_SETICONSPACING, 0, 7864440);
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
			if (this.listView1.SelectedItems.Count == 0)
			{
				YJKMessageBox.Show("请选择族类型");
				return;
			}
			this.m_strFamilyName = this.listView1.SelectedItems[0].Text;
			if (this.m_strFamilyName == null || this.m_strFamilyName == "")
			{
				YJKMessageBox.Show("请选择族类型");
				return;
			}
			string str = this.m_strFamilyName + "_";
			string path = Path.Combine(this.m_strFullPath, this.m_strImageName + "截图");
			string text = Path.Combine(path, str + "3.jpg");
			string text2 = Path.Combine(path, str + "2.jpg");
			this.Image3D = Image.FromFile(text, true);
			this.Image2D = Image.FromFile(text2, true);
			this.Image2DPath = text2;
			this.Image3DPath = text;
			base.Close();
		}

		private void listBox_MeasureItem(object sender, MeasureItemEventArgs e)
		{
			e.ItemHeight += 12;
		}

		private void ChoiceFamilyForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void listView1_DoubleClick(object sender, EventArgs e)
		{
			this.m_strFamilyName = this.listView1.SelectedItems[0].Text;
			string str = this.m_strFamilyName + "_";
			string path = Path.Combine(this.m_strFullPath, this.m_strImageName + "截图");
			string text = Path.Combine(path, str + "3.jpg");
			string text2 = Path.Combine(path, str + "2.jpg");
			this.Image3D = Image.FromFile(text, true);
			this.Image2D = Image.FromFile(text2, true);
			this.Image2DPath = text2;
			this.Image3DPath = text;
			base.Close();
		}

		private Document m_doc;

		private BuiltInCategory m_builtInCategory;

		public string m_strImageName;

		public string m_strFamilyName;

		public ChoiceFamilyForm.ImageDefault m_imageDefault;

		private Image m_Image2D;

		private string m_Image2DPath;

		private Image m_Image3D;

		private string m_Image3DPath;

		private string m_strFullPath;

		private int LVM_SETICONSPACING = 4149;

		private int m_nSelectIndex;

		public delegate void ImageDefault();
	}
}
